package com.gemme.gemme.wode;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.util.DpUtil;
import com.gemme.gemme.util.QRCodeUtil;
import com.gemme.gemme.view.SwitchView;
import com.gemme.gemme.view.Topview;
import com.google.android.material.tabs.TabLayout;
import com.jaeger.library.StatusBarUtil;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//侧滑删除地址
public class TuiJianguanActivity extends BaseActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tjg);
        initTop();
        initView();
        initData();
    }

    private void initData() {
        getCode();
        getLIst();
    }

    private void getCode() {
        String url = DataManager.BaseUrl + "user/recommend/findinvite" ;
        SharedPreferences MSP = getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
        String access_token = MSP.getString(Constant.ACCESS_TOKEN, "").trim();

        if(TextUtils.isEmpty(access_token))
            return;

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,"Bearer " + access_token);
        DataManager.toServer2(mContext, url, DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    System.out.println("");
                    String code = (new JSONObject(response)).getString("data");
                    if (TextUtils.isEmpty(code))
                        return;
                    tv_code.setText("我的邀请码 : " + code);
                    Bitmap bitmap = QRCodeUtil.createQRCodeBitmap(code, DpUtil.dp2px(mContext,120), DpUtil.dp2px(mContext,120));
                    iv_qrcode.setImageBitmap(bitmap);
                    switchview.setState(true);
                }catch (Exception e){

                }
            }
        });
    }

    private void getLIst() {
        String url = DataManager.BaseUrl + "user/recommend/directlist" ;
        SharedPreferences MSP = getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
        String access_token = MSP.getString(Constant.ACCESS_TOKEN, "").trim();

        if(TextUtils.isEmpty(access_token))
            return;

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,"Bearer " + access_token);
        DataManager.toServer2(mContext, url, DataManager.POST_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    System.out.println("");
                    Directlist directlist = JSON.parseObject(response, Directlist.class);
                    List<Directlist.DataBean.RecordsBean> records = directlist.getData().getRecords();
                    System.out.println("");
                    rvResult.setAdapter(new TuijianGuanAdapter(records));
                }catch (Exception e){

                }
            }
        });
    }

    ImageView iv_qrcode;
    TextView tv_code;
    SwitchView switchview;
    RecyclerView rvResult;
    private void initView() {
//        createQRCodeBitmap("你好呀", 800, 800,"UTF-8","H", "1", Color.GREEN, Color.WHITE);

//        Bitmap bitmap = QRCodeUtil.createQRCodeBitmap("你好呀", 800, 800);
        iv_qrcode = findViewById(R.id.iv_qrcode);
//        iv_qrcode.setBackground(bitmap);

        tv_code = findViewById(R.id.tv_code);
        rvResult = findViewById(R.id.rvResult);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        rvResult.setLayoutManager(mLayoutManager);



        switchview = findViewById(R.id.switchview);
        switchview.setOnSwitchChange(new SwitchView.OnSwitchChange() {
            @Override
            public void OnSwitchChange(boolean isOn) {
                if(isOn){
                    toTuijianguan();
                }
            }
        });
    }

    private void toTuijianguan() {
        String url = DataManager.BaseUrl + "user/recommend/saverecommend" ;
        SharedPreferences MSP = getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
        String access_token = MSP.getString(Constant.ACCESS_TOKEN, "").trim();

        if(TextUtils.isEmpty(access_token))
            return;

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,"Bearer " + access_token);
        DataManager.toServer2(mContext, url, DataManager.POST_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    System.out.println("");
                    switchview.setState(true);
                    Toast.makeText(mContext, "成为推荐官成功", Toast.LENGTH_SHORT).show();
                }catch (Exception e){

                }
            }
        });
    }

    class TuijianGuanAdapter extends RecyclerView.Adapter<TuijianGuanAdapter.ViewHolder>{

        private List<Directlist.DataBean.RecordsBean> adapterList;

        public TuijianGuanAdapter(List<Directlist.DataBean.RecordsBean> adapterList){

            this.adapterList = adapterList;
        }

         @NonNull
         @Override
         public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
             //adapter_address
             View inflate = LayoutInflater.from(parent.getContext()).
                     inflate(R.layout.tuijianguan_view, null);
             LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
             inflate.setLayoutParams(layoutParams);
             return new TuijianGuanAdapter.ViewHolder(inflate);
         }

         @Override
         public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            try {
                Directlist.DataBean.RecordsBean recordsBean = adapterList.get(position);
                holder.tv_name.setText(recordsBean.getUsername());
                holder.tv_time.setText(recordsBean.getUpdateTime());
            }catch (Exception e){

            }
         }

         @Override
         public int getItemCount() {
             return adapterList.size();
         }

         public class ViewHolder extends RecyclerView.ViewHolder {
             TextView tv_name;
             TextView tv_time;
             public ViewHolder(@NonNull View itemView) {
                 super(itemView);

                 tv_name = itemView.findViewById(R.id.tv_name);

                 tv_time = itemView.findViewById(R.id.tv_time);
             }
         }
     }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("推荐官");
    }




}